package cn.rkylin.oms.warehouse.shiporder.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.warehouse.shiporder.dao.ShiporderDAO;
import cn.rkylin.oms.warehouse.shiporder.domain.ShiporderSku;
import cn.rkylin.oms.warehouse.shiporder.vo.ShiporderVO;

/**
 * 发货Service
 *
 * @author 齐涛
 * @version 1.0
 * @created 28-9-2017 10:51:30
 */
@Repository(value = "shiporderService")
public class ShiporderServiceImpl extends ApolloService implements IShiporderService {

	@Autowired
	private ShiporderDAO shiporderDAO;

	@Value("${oms.address}")
	private String omsAddres;

	/**
	 * 查询发货单列表
	 *
	 * @param shiporderVO
	 *            发货单VO
	 * @return 发货单信息
	 * @throws Exception
	 *             异常
	 */
	public List<ShiporderVO> selectShiporderList(ShiporderVO shiporderVO) throws Exception {
		return shiporderDAO.selectShiporderList(shiporderVO);
	}

	/**
	 * 查询发货单列表为取消
	 * 
	 * @param shiporderVO
	 *            发货单VO
	 * @return 发货单信息
	 * @throws Exception
	 *             异常
	 */
	public List<ShiporderVO> selectShiporderListForCancel(ShiporderVO shiporderVO) throws Exception {
		return shiporderDAO.selectShiporderListForCancel(shiporderVO);
	}

	/**
	 * 查询发货单商品列表
	 * 
	 * @param shiporderSku
	 *            发货单商品
	 * @return 发货单商品列表
	 * @throws Exception
	 *             异常
	 */
	public List<ShiporderSku> selectByVOShiporderSku(ShiporderSku shiporderSku) throws Exception {
		return shiporderDAO.selectByVOShiporderSku(shiporderSku);
	}

	/**
	 * 查询发货单信息
	 *
	 * @param shiporderVO
	 *            发货单VO
	 * @return 发货单信息
	 * @throws Exception
	 *             异常
	 */
	public ShiporderVO getShiporderInfo(ShiporderVO shiporderVO) throws Exception {
		return shiporderDAO.getShiporderInfo(shiporderVO);
	}

	/**
	 * 修改查询发货
	 *
	 * @param shiporderVO
	 *            发货单VO
	 * @throws Exception
	 *             异常
	 */
	public void updateShiporder(ShiporderVO shiporderVO) throws Exception {
		shiporderDAO.updateShiporder(shiporderVO);
	}

	/**
	 * 系统发货单主表信息插入数据库
	 *
	 * @param shiporderVO
	 * @return
	 * @throws Exception
	 */
	public int insertSelectiveShiporder(ShiporderVO shiporderVO) throws Exception {
		return shiporderDAO.insertSelectiveShiporder(shiporderVO);
	}

	/**
	 * 系统发货单商品明细表信息插入数据库
	 *
	 * @param shiporderSku
	 * @return
	 * @throws Exception
	 */
	public int insertSelectiveShiporderSku(ShiporderSku shiporderSku) throws Exception {
		return shiporderDAO.insertSelectiveShiporderSku(shiporderSku);
	}

	/**
	 * 查询需要平台发货的信息
	 * 
	 * @param shopVO
	 * @return
	 * @throws Exception
	 */
	public List<ShiporderVO> selectByShopVoShipOrderConsign(cn.rkylin.oms.system.shop.vo.ShopVO shopVO)
			throws Exception {
		return shiporderDAO.selectByShopVoShipOrderConsign(shopVO);
	}

	/**
	 * 平台发货
	 * 
	 * @return
	 * @throws Exception
	 */
	public Map<String, String> pushOfflineSend(String shopId,
			cn.rkylin.oms.warehouse.shiporder.request.RequestTradeOfflineSend request) throws Exception {

		// 将shopId赋值到请求实体中
		request.setShopId(shopId);

		// 请求参数拼接
		String postParams = JSON.toJSONString(request);

		// 请求地址
		String requestUrl = omsAddres + "pushTrade/pushTradeOfflineSend.action";

		// 请求数据
		String resJson = cn.rkylin.apollo.common.util.HttpUtils.sendByPostJson(requestUrl, postParams);

		// 转换响应数据
		Map<String, String> resultMap = JSON.parseObject(resJson, Map.class);

		return resultMap;
	}

	/**
	 * 更新
	 */
	public int updateByPrimaryKeySelectiveShipOrder(ShiporderVO shiporderVO) throws Exception {
		return shiporderDAO.updateByPrimaryKeySelectiveShipOrder(shiporderVO);
	}

}
